<?
class dbdRouter
{
	private $request = null;
	private $controller = null;
	private $action = null;
	private $baseUrl = null;
	private $params = array();

	public function __construct(dbdRequest $request)
	{
		$this->request = $request;
		$this->parseRequest();
		$this->buildURL();
	}

	private function parseRequest()
	{
		dbdURI::set($this->request->get("REDIRECT_URL"));
		$this->setController(dbdURI::getController());
		$this->setAction(dbdURI::getAction());
		foreach (dbdURI::getParams() as $k => $v)
			$this->setParam($k, $v);
	}

	private function buildURL()
	{
		$this->baseUrl = dbdURI::create($this->controller, $this->action);
	}

	public function setController($controller)
	{
		$this->controller = $controller;
		$this->buildURL();
		return $this->controller;
	}

	public function setAction($action)
	{
		$this->action = $action;
		$this->buildURL();
		return $this->action;
	}

	public function setParam($name, $value)
	{
		$this->params[$name] = $value;
		return $this->params[$name];
	}

	public function addParam($name, $value)
	{
		if (!is_array($value))
			$value = array($value);
		$cur = $this->getParam($name);
		if (empty($cur))
			$cur = array();
		if (!is_array($cur))
			$cur = array($cur);
		return $this->setParam($name, array_merge($cur, $value));
	}

	public function getController()
	{
		return $this->controller;
	}

	public function getAction()
	{
		return $this->action;
	}

	public function getParam($name)
	{
		if (isset($this->params[$name]))
			return $this->params[$name];
		else
			return $this->request->get($name);
	}

	public function getParams()
	{
		$params = $this->params;
		$params = array_merge($params, $this->request->getQuery());
		$params = array_merge($params, $this->request->getPost());
		return $params;
	}

	public function getURL($get_params = false)
	{
		if ($get_params)
			return dbdURI::create($this->getController(), $this->getAction(), $this->getParams());
		return $this->baseUrl;
	}
}
?>